/**
  ETFAna project, Anyang Normal University && IMP-CAS
  \class ETFVPID
  \brief class for particle identification. This class takes tracks on both sides
  of the dipole magnet to extract particle A/Z, and particle velocity beta.
  \author SUN Yazhou, asia.rabbit@163.com
  \since 2021-11-10
  \date 2021-12-15 last modified
  \attention
  changelog
  <table>
  <tr>  <th>Date         <th>Author      <th>Description                   </tr>
  <tr>  <td>2021-11-10   <td>Asia Sun    <td>file created                  </tr>
  </table>

  \copyright Copyright (c) 2021-2024 Anyang Normal U. && IMP-CAS with LGPLv3 LICENSE
*/

#include <iostream>
#include <TGraph.h>
#include <TLegend.h>
#include <TClonesArray.h>
#include "ETFRun.h"
#include "ETFVPID.h"
#include "ETFParaManager.h"
#include "ETFTrackManager.h"
#include "ETFDetectorManager.h"
#include "ETFTaTracker.h"
#include "ETFPostMagTracker.h"
#include "ETFGlobalTracker.h"
#include "ETFTrackTa4.h"
#include "ETFTrackTa.h"
#include "ETFTrack.h"
#include "ETF3DTrack.h"
#include "ETFTOFWall.h"
#include "ETFTimer2.h"
#include "ETFTimer1.h"
#include "ETFMath.h"
#include "ETFMsg.h"
#include "ETFPlaHit.h"
#include "ETFYaml.h"

using std::cout;
using std::endl;

double ETFVPID::kB = -999.;
// const double ETFVPID::L = 25978.; // distance from Tstart to Tstop in mm
const double ETFVPID::L = 25881.; // distance from Tstart to Tstop in mm
const double ETFVPID::c0 = ETFMath::c0(); // in mm/ns
const double ETFVPID::u0 = ETFMath::uMeV(); // in MeV
// CC = 0.321840605 = e0/(u0*c0*1E6) SI unit
const double ETFVPID::CC = ETFMath::CC(); // 0.32184060506023993;
// the z border of the effective magField (in mm)
const double ETFVPID::kzMagIn = ETFMath::zMagIn();
const double ETFVPID::kzMagOut = ETFMath::zMagOut();
const double ETFVPID::HPI = ETFMath::Pi() / 2.;
const double ETFVPID::PI = ETFMath::Pi();

#define sum2 ETFMath::Sum2

ETFVPID::ETFVPID() : fGCurve(nullptr), fGLegend(nullptr){}

ETFVPID::~ETFVPID(){
  if(fGCurve) delete fGCurve;
  if(fGLegend) delete fGLegend;
} // end dtor

// assign all the managers
void ETFVPID::Configure(){
  fRun = ETFRun::Instance();
  fTree = fRun->GetTree();
  fpm = ETFParaManager::Instance();
  fdm = ETFDetectorManager::Instance();
  ftm = ETFTrackManager::Instance();

  ftof2_ofst = 0.;
  if((*fpm->GetDelayPara())["tof2_ofst"])
    ftof2_ofst = (*fpm->GetDelayPara())["tof2_ofst"].as<double>();
  fTaZ = fdm->GetTargetZ();
  if(-999. == fTaZ) ETFMsg::Error("ETFPIDm", "ctor: target z position not assigned.");
  fTstopZ = fdm->GetDetector("tstop", true)->GetZ();
} // end member function Configure

void ETFVPID::Initialize(){
  fHasFlied = false;
  fFlySuccess = false;
} // end member function Initialize

void ETFVPID::Draw(){
  ConstructGraph();
  if(fGCurve) fGCurve->Draw("psame");
  if(fGLegend) fGLegend->Draw();
} // end member function Draw

void ETFVPID::ConstructGraph(){
  // draw the arc
  if(!fGCurve){
    fGCurve = new TGraph();
    fGCurve->SetMarkerStyle(1); fGCurve->SetMarkerSize(3);
    fGCurve->SetMarkerColor(6); fGCurve->SetLineColor(6);
  } // end if
  if(!fGLegend){ // mark out the aoz value of the event
    fGLegend = new TLegend(0.4, 0.8, 0.6, 0.89);
    fGLegend->SetBorderSize(0);
  } // end if
  else if(fGLegend->GetNRows()) fGLegend->Clear();
  while(fGCurve->GetN()) fGCurve->RemovePoint(0); // clear the graph
} // end member function ConstructGraph

void ETFVPID::Print(){
  // cout << "\033[32;1m============= VPID PRINT =============\033[0m" << endl;
} // end member function Print

// \return event index
int ETFVPID::Index() const{
  return fdm->Index();
} // end member function index
